<template>
  <div class="con">
    <MyHeader></MyHeader>
    <MyTable :list="list">
      <template #header>
        <th>#</th>
        <th>商品名称</th>
        <th>价格</th>
        <th>标签</th>
        <th>操作</th>
      </template>
      <template #body="scope">
        <td>{{ scope.row.id }}</td>
        <td>{{ scope.row.goods_name }}</td>
        <td>{{ scope.row.goods_price }}</td>
        <td>
          <span>
            <input
              @keydown.esc="scope.row.inputVisible = false"
              v-model.trim="scope.row.inputValue"
              @keydown.enter="enterFn(scope.row)"
              @blur="scope.row.inputVisible = false"
              v-foc
              v-if="scope.row.inputVisible"
              style="width: 100px"
              type="text"
            />
            <button
              v-else
              @click="scope.row.inputVisible = true"
              style="display: block"
            >
              +Tab
            </button>
          </span>
          <span
            class="badge bg-warning"
            style="margin-left: 5px"
            v-for="(itme, index) in scope.row.tags"
            :key="index"
          >
            {{ itme }}</span
          >
        </td>
        <td>
          <button @click="del(scope.row.id)" class="btn btn-danger">
            删除
          </button>
        </td>
      </template>
    </MyTable>
  </div>
</template>

<script>
import MyHeader from "../components/MyHeader.vue";
import MyTable from "../components/MyTable.vue";
export default {
  components: {
    MyHeader,
    MyTable,
  },
  name: "Zuoye02Mygoodslist",

  data() {
    return {
      list: [],
    };
  },

  mounted() {},

  methods: {
    del(id) {
      let index = this.list.findIndex((itme) => (itme.id = id));
      this.list.splice(index, 1);
    },
    enterFn(obj) {
      if (obj.inputValue == "") {
        return alert("内容不能为空");
      }
      obj.tags.push(obj.inputValue);
      obj.inputValue = "";
    },
  },

  async created() {
    let res = await this.$http({
      url: "/api/goods",
    });
    console.log(res);
    this.list = res.data.data;
  },
};
</script>

<style lang="less" scoped>
.con {
  padding-top: 45px;
}
</style>